package test21.homeWork;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Homework03Client {
    public static void main(String[] args) throws IOException {
        //接收用户输入，指定下载文件名
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入下载文件名");
        String downloadFileName = scanner.next();
        //客户端连接服务端，准备发送
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        //发送下载的文件名
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(downloadFileName.getBytes());
        socket.shutdownOutput();

        //读取服务器返回的文件
        BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
        byte[] array = StreamUtils.streamToByteArray(bufferedInputStream);
        // 得到一个输出流，准备将 array 写入到磁盘文件
        String filePath = "d:\\" + downloadFileName + ".mp3";
        BufferedOutputStream bufferedOutputStream1 = new BufferedOutputStream(new FileOutputStream(filePath));
        bufferedOutputStream1.write(array);

        bufferedOutputStream1.close();
        bufferedInputStream.close();
        outputStream.close();
        socket.close();
        System.out.println("客户端下载完毕，正确退出..");
    }
}
